#!/opt/python3.3-beta/bin/python3
import socket

print ("THIS STILL REQUIRES FINISHING, ALL THE FOR LOOPS AND IF STATEMENTS ARE FINISHED, BUT IT STILL REQUIRES THE SUBNET TARGETS TO BE GENERATED")
print ("ALSO NEED TO BE ABLE TO SPECIFY THE TARGET LIST")
file = open('targets.txt')

for line in file:
	#remove trailing and beginning spaces from string
	ipString = line.rstrip()
	ipString = ipString.lstrip()
	# looks for subnet listings using prefix format
	prefixCount = ipString.count('/')
	# looks for the subnet listings using the netmask format	
	spaceCount = ipString.count(' ')
	
	# PREFIX - e.g. 127.0.0.1/24
	if prefixCount == 1 and spaceCount == 0:
		#print ('subnet prefix')	
		# split at the /, then check a valid IP address has been supplied
		ip_prefix = ipString.split('/')
		if ip_prefix[1].isdigit():
			if int(ip_prefix[1]) > 32 or int(ip_prefix[1]) < 0:
				print ("Error: Not a valid subnet mask: " + line)
			else:
				try:
					socket.inet_aton(ip_prefix[0])
					# do the nmap thing here
					print (line)
				except socket.error:
					print ("Error: Not a valid IP address: " + line )
		else:
			print ("Error: Not a valid subnet mask: " + line)

	#e.g. 127.0.0.1/24 192.168.0.1/14
	elif prefixCount > 1:
		print ("Error: Too many / have been specified: " + line)

	#NETMASK - e.g. 127.0.0.1 255.255.255.0
	elif spaceCount == 1 and prefixCount == 0:
		ipValues = ipString.split()
		try:
			socket.inet_aton(ipValues[0])
			# do the ipcalc and nmap stuff here
			#print (line)
			
			try:
				socket.inet_aton(ipValues[1])
				print (line)
			except socket.error:
				print ("Error: Not a valid subnet mask: " + line)
		except socket.error:
			print ("Error: Not a valid IP address: " + line)

	#JUST IP - looks for just an IP address
	elif spaceCount == 0 and prefixCount == 0:
		try: 
			socket.inet_aton(ipString)
			print (line)
		except socket.error:
			print ("Error: Not a valid IP address: " + line)
		
	# error for all other conditions
	else: 	
		print ("Error: Has not matched a valid IP rule: " + line)

